package sort.test4_7;

import sort.test3_27.QuickSort1;

public class QuickSort {

    public int[] quickSort(int[] nums) {
        if (nums.length == 0) {
            return null;
        }
        sort(nums, 0, nums.length - 1);
        return nums;
    }

    public void sort(int[] nums, int lo, int hi) {
        if (lo >= hi) {
            return;
        }
        int partion = partion(nums, lo, hi);
        sort(nums, lo, partion - 1);
        sort(nums, partion + 1, hi);
    }


    public int partion(int[] nums, int lo, int hi) {
        int part = nums[lo];
        int j = lo + 1;
        int k = hi;
        while (j <= k) {
            while (nums[j] < part) {
                j++;
            }
            while (nums[k] > part) {
                k--;
            }
            if (j >= k) {
                break;
            }
            swap(nums, j, k);
        }
        swap(nums, lo, k);
        return k;
    }


    public void swap(int[] nums, int j, int k) {
        int temp = nums[j];
        nums[j] = nums[k];
        nums[k] = temp;
    }

    public static void main(String[] args) {
        QuickSort quickSort = new QuickSort();
        int[] nums = {2,3,1,4,6,4,7,12};
        quickSort.sort(nums, 0, nums.length - 1);
        for (int a : nums
        ) {
            System.out.println(a);
        }
    }


}
